
const path = require("path")
const md5 = require("md5")
const sql = require("../module")
const login = {
}
login.views = (req, res) => {
    res.sendFile(path.resolve(__dirname, "../views/login.html"))
}
//  查询有没有这个账号， 有就设置req。session 返回成功, 没有就返回失败
//  连接数据库

login.login = (req, res) => {
    let { p, u } = req.body

    let str = `SELECT * from userinfo WHERE username = '${u}'  and \`password\` = '${md5(p)}'`
    sql.query(str).then(([data]) => {
        console.log(data);
        if (data.length == 1) {
            req.session.userinfo = data[0] // 赋予用户身份
            res.json({
                code: 2000,
                message: "登录成功"
            })
        } else {
            res.json({
                code: 4004,
                message: "账号密码错误"
            })
        }
    })

}




module.exports = login